<?php
/**
 * CodeIgniter - Controller - Welcome
 *
 * An open source application development framework for PHP 4.3.2 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Controller - Online Test
 *
 * @package		CodeIgniter
 * @subpackage	Controller
 * @category	Controller
 * @author		PHPTest
 * @link		NA
 */
 
class Online_Test extends Controller 
{
	var $view_array = array();
	var $view_item 	= 'welcome_message';	
	var $page_title = 'Online Test - PHP, Mysql, CSS, JavaScript';
	var $table_name = TABLE_USER;
	
	// --------------------------------------------------------------------------
	
	/** 
	 * Online Test - Constructor
	 *
	 * @access 		public
	 * @category	Function
	 * @param  		NA	 
	 * @return 		void
	 */	
	function Online_Test()
	{
		parent::Controller();		
		$this->session->sess_destroy();
	}
	
	// --------------------------------------------------------------------------
	
	/** 
	 * Index
	 *
	 * @access 		public
	 * @category	Function
	 * @param  		NA	 
	 * @return 		void
	 */	
	function index()
	{
		show_page($this->view_item);
    }	
	
	// ------------------------------------------------------------------------	
		
	/**
	 * login
	 *
	 * @access 		public
	 * @category	Function
	 * @return 		mixed
	 *
	 */
	function login()
	{	
		if(!$_POST)	
			redirect('online_test');
		
		$login_name = $this->input->post('txt_login_name');
		$login_pwd 	= $this->input->post('txt_login_pwd');	
		
		$where = array();
		$where['user_login'] 	= $login_name;
		$where['user_pwd'] 		= md5($login_pwd);
		$query=$this->db->get_where($this->table_name,$where);
		if($query->num_rows()>0)
		{
			// return result set as an associative array			
			$user_data = $query->row_array();
			$this->session->set_userdata('user_data', $user_data);				
			redirect('/questions');		
		}
		else
		{
			$this->view_array['errors'] = '<li>Oops!! You have entered an invalid login/password. Kindly try again!!</li>'; 	
			show_page($this->view_item,$this->view_array);
		}	
	}
	
	// ------------------------------------------------------------------------	
		
	/**
	 * Register
	 *
	 * @access 		public
	 * @category	Function
	 * @return 		mixed
	 *
	 */
	function register()
	{	
		if(!$_POST)	
			redirect('online_test');
			
		$reg_name 		= $this->input->post('txt_reg_name');
		$reg_pwd 		= $this->input->post('txt_reg_pwd');
		$reg_conf_pwd 	= $this->input->post('txt_reg_conf_pwd');
		
		$errors = $this->_validate_form();
		
		if(empty($errors))
		{
			$data = array();
			$data['user_login'] 	= $this->input->post('txt_reg_name');
			$data['user_pwd'] 		= md5($this->input->post('txt_reg_pwd'));
			$data['user_created'] 	= date('Y-m-d H:i:s');
			$data['user_profile'] 	= 1;
			$data['user_status'] 	= 1;
			
			$this->db->insert($this->table_name, $data); 
			$success	= 'Congratulations, you been successfully registered!!!<br />Login to enter.';
			$this->view_array['success'] = $success; 
		}			
		else
		{
			$this->view_array['errors'] = $errors; 
		}
		
		show_page($this->view_item,$this->view_array);	
	}
	
	// --------------------------------------------------------------------------
	
	/**
  	 * Validate Form
	 *
	 * @access 		private	
	 * @category	Function 
	 * @return 		mixed
	 */
	function _validate_form()
	{
		$this->load->library('Form_validation');
		
		$rules = array(
		   array(
                     'field'   => 'txt_reg_name',
                     'label'   => 'Registration Name',
                     'rules'   => 'trim|required|min_length[5]|max_length[10]|xss_clean'
                  ),
		   array(
                     'field'   => 'txt_reg_pwd',
                     'label'   => 'Registration Password',
                     'rules'   => 'trim|required|min_length[5]|max_length[10]|xss_clean'
                  ),
	       array(
                     'field'   => 'txt_reg_conf_pwd',
                     'label'   => 'Registration Confirm Password',
                     'rules'   => 'trim|required|min_length[5]|max_length[10]|xss_clean'
                  )		  					  	  
			 
	           );	
			
		$this->form_validation->set_rules($rules);
		$this->form_validation->set_error_delimiters('<li>','</li>');	
		
		if ($this->form_validation->run() == FALSE)
		{
			$errors 		= $this->form_validation->error_string();
			$reg_pwd 		= $this->input->post('txt_reg_pwd');
			$reg_conf_pwd 	= $this->input->post('txt_reg_conf_pwd');
			
			if($reg_pwd != $reg_conf_pwd)
				$errors .= '<li>Password and Confirm Password do not match</li>';
				
			return $errors;
		}
		else
		{
			return 0;
		}
		
	}
}

//EOF Controller Online Test
?>